﻿using Dsl.Ped.Common;
using Dsl.Ped.Framework.Common;
using Dsl.Ped.Framework.Helpers;

namespace Dsl.Ped.Editor.Modules.ProjectiveArea
{
    /// <summary>
    /// View model of the projective area
    /// </summary>
    public class ProjectiveAreaViewModel : ViewModelBase
    {
        #region Fields

        /// <summary>
        /// Name of the currently loaded XML file
        /// </summary>
        private string xmlFileName { get; set; }

        /// <summary>
        /// Name of the currently loaded XSD file
        /// </summary>
        private string xsdFileName { get; set; }

        /// <summary>
        /// Get the object which provides projections
        /// </summary>
        private IProjective projectiveObject { get; set; }

        /// <summary>
        /// Hashcode of the <see cref="IProjective"/> object. It is necessary
        /// to keep the hashcode in order to decide whether the object has been changed (is dirty)
        /// </summary>
        private string originalObjectHash { get; set; }

        private string currentObjectHash { get; set; }

        public EditorResources EditorResources { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectiveAreaViewModel"/> class.
        /// </summary><param name="editorResources">The editor resources.</param>
        public ProjectiveAreaViewModel(EditorResources editorResources)
        {
            this.EditorResources = editorResources;
           
            initialize();
        }

        #endregion

        #region Members

        /// <summary>
        /// Current text content of the file
        /// </summary>
        public string XMLFileContent { get; set; }

        /// <summary>
        /// Controller of the lifetime of the ProjectiveObject
        /// </summary>
        public IProjective ProjectiveObject
        {
            get
            {
                return projectiveObject;
            }
            set
            {
                projectiveObject = value;
                var instance = (object)projectiveObject;
                originalObjectHash = HashingHelper.GetHash(instance);
                
                RaisePropertyChanged(() => ProjectiveObject);
            }
        }

        /// <summary>
        /// Gets or sets the indication that the projective object loaded into the projective area has changed
        /// </summary>
        public bool IsDirty
        {
            get
            {
                var instance = (object)projectiveObject;
                currentObjectHash = HashingHelper.GetHash(instance);
                var result = !originalObjectHash.Equals(currentObjectHash);
                return result;
            }
            set
            {
                if (value == false)
                {
                    originalObjectHash = currentObjectHash;
                }
            }
        }

        #endregion 

        #region Methods

        /// <summary>
        /// Set the projective object to display within the editor
        /// </summary>
        public void SetProjectiveObject(IProjective projectiveObject)
        {
            ProjectiveObject = projectiveObject;
        }

        /// <summary>
        /// Initialize the model 
        /// </summary>
        private void initialize()
        {
            // Initialize fields
            xmlFileName = "";
            XMLFileContent = "";

            // Set the default projection
            projectiveObject = null;
        }

        #endregion
    }
}
